package com.andlabs.gd.simulation;

import java.util.List;

import android.util.Log;

import com.andlabs.gd.base.GameActivity;
import com.andlabs.gd.base.SimulationListener;
import com.andlabs.gd.communication.CommunicationListener;
import com.andlabs.gd.map.GameMap;
import com.andlabs.gd.objects.Building;
import com.andlabs.gd.objects.Character;
import com.andlabs.gd.objects.Hero;

/**
 * @author johannes
 */
public class Simulation implements SimulationListener, CommunicationListener {

	boolean mIsLoading = true;
	public Hero mHero;
	private GameActivity mContext;
	public GameMap mMap;
	
	public Simulation(GameActivity context){
		mContext = context;
	}
	
	@Override
	public void simulationIteration(float delta) {
		updateHero(delta);	
	}

	@Override
	public void initializeSimulation() {
		Log.d("Test", "initializing simulation");
		mIsLoading = false;
		mHero = new Hero(mContext);
	}

	@Override
	public boolean isInitializingSimulation() {
		return mMap == null;
	}	

	@Override
	public void getCharacter(java.lang.Character character) {
	}

	@Override
	public void setBuildings(List<Building> buildings) {
	}

	@Override
	public void setCharacter(List<java.lang.Character> character) {
	}

	@Override
	public void setMap(GameMap map) {
		synchronized(map){
			mMap = map;
		}
	}
	
	private int mLastTouchX, mLastTouchY;
	public void updateHero(float delta){
		synchronized(mHero){
        	if(mContext.getTouchX() != mHero.mDestinationX && ! mContext.mIsMoving && mContext.getTouchX() != 0 && mContext.getTouchX() != mLastTouchX){
        		mHero.mDestinationX += mContext.getTouchX() - mHero.mPermanentX;
        		mLastTouchX = mContext.getTouchX();
			}
			if(mContext.getTouchY() != mHero.mDestinationY && ! mContext.mIsMoving && mContext.getTouchY() != 0 && mContext.getTouchY() != mLastTouchY){
				mHero.mDestinationY += mContext.getTouchY() - mHero.mPermanentY /* (int) (96 / mZoomLevel)*/;
				mLastTouchY = mContext.getTouchY();
			}
        	mHero.update(delta);
        }		
	}
}

